package whu.cn.whushare.ui.main.my.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;

import java.util.List;

import whu.cn.whushare.R;
import whu.cn.whushare.bean.User;
import whu.cn.whushare.common.ImageWorkerManager;
import whu.cn.whushare.ui.main.known.bean.AllQuestion;
import whu.cn.whushare.util.Utils;

/**
 * Created by xiaoping on 2015/4/22.
 */
public class TopicAllQuestionAdapter extends BaseAdapter {
        private List<AllQuestion> questions; // 数据

        private Context context;

        /**
         *
         * @param context
         *            mainActivity
         * @param questions
         *            显示的数据
         */
         public  TopicAllQuestionAdapter(Context context, List<AllQuestion> questions) {
            this.context = context;
            this.questions = questions;

        }

        /*
         * 获得数据总数
         */
        @Override
        public int getCount() {
            return questions.size();
        }

        /*
         * 根据索引为position的数据
         */
        @Override
        public Object getItem(int position) {
            return questions.get(position);
        }

        /*
         * 根据索引值获得Item的Id
         */
        @Override
        public long getItemId(int position) {
            return position;
        }

        /*
         * 通过索引值position将数据映射到视图convertView具有缓存功能，在第一页时为null，在第二第三....页时不为null
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.layout_my_topic_detail_item, null);
                viewHolder = new ViewHolder();
                ViewMappingUtil.mapView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            AllQuestion all_question = questions.get(position);
            viewHolder.questionName.setText(all_question.getQuestion().getQuestionName() );
            viewHolder.answer_question_count.setText("回答:"+all_question.getAnswerNum()+ "");
            viewHolder.answer_time.setText(all_question.getLastModifyTime() + "");
            viewHolder.attentionNum.setText("关注："+all_question.getAttentionNum()+"");
            User user = all_question.getQuestionUser();
            viewHolder.userName.setText(user.getNickName());
            if (!TextUtils.isEmpty(user.getAvatar())) {
                ImageWorkerManager.getInstance(context).loadCommonImage(Utils.getPhotoUrl(user.getAvatar()),
                        viewHolder.avatar, R.drawable.avatar, R.drawable.avatar);
            }
            return convertView;
        }

        class ViewHolder {
            @ViewMapping(id = R.id.question)
            private TextView questionName;
            @ViewMapping(id = R.id.answer_count)
            private TextView answer_question_count;
            @ViewMapping(id = R.id.end_time)
            private TextView answer_time;
            @ViewMapping(id = R.id.user_avatar)
            private ImageView avatar;
            @ViewMapping(id = R.id.other_user_name1)
            private TextView userName;
            @ViewMapping(id=R.id.attention_num)
            private TextView attentionNum;
            @ViewMapping(id = R.id.question_user_layout)
            private View userLayout;
        }
 }

